package com.wuhaha.libcommon;

import android.app.Application;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

//专门获取Application对象

//ActivityThread是主线程的一个入口   里面有一个方法是currentApplication 这个方法就是返回Application
public class AppGlobals {
    private static Application application;



    public static Application getApplication(){
        if (application == null){
            try {
                Method currentApplication =
                        Class.forName("android.app.ActivityThread").getDeclaredMethod("currentApplication");
                application = (Application) currentApplication.invoke(null, (Object[]) null);
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
        }
        return application;
    }
}
